package com.arc.spring.service.impl;

import com.arc.spring.dao.UserDao;
import com.arc.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author YangYunhe
 * @date 2024/12/29
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    /**
     * @Autowired
     * 根据类型去容器中寻找对应的对象，找到后给当前属性赋值
     * 不需要依赖 set() 方法
     * 属性类型可以是接口，会自动匹配对应的实现类对象
     *
     * 多个实现类都可以注入，则需要使用 @Qualifier 唯一指定
     */

    @Autowired
    @Qualifier("userDaoImpl1")
    private UserDao userDao1;

    /**
     * @Resource 与 @Autowired 作用相同
     * @Resource(name = "userDaoImpl2") = @Autowired + @Qualifier("userDaoImpl2")
     */
    @Resource(name = "userDaoImpl2")
    private UserDao userDao2;

    /**
     * 基础类型属性赋值
     */
    @Value("root")
    private String name;
    @Value("7")
    private Integer age;
    @Value("${driverClassName}")
    private String driverClassName;

    @Override
    public int addUser(int userId, String userName) {
        userDao1.addUser(userId, userName);
        userDao2.addUser(userId, userName);
        System.out.println(name);
        System.out.println(age);
        System.out.println(driverClassName);
        return 1;
    }

}
